package com.abigdreamer.arkflow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.tree.DefaultAttribute;

public class JpdlToJsonService {

	private int count = 0;
	
	private Map<String, String> nodeNameMap = new HashMap<String, String>();
	
	private List<String> toNodeJson(Element root) {
		List<String> result = new ArrayList<String>();
		
		for (Object element : root.elements()) {
			Element node = (Element) element;
			String[] positionInfo = node.attributeValue("g").split(",");
			List<DefaultAttribute> props = node.attributes();
			String propString = "";
			for (DefaultAttribute prop : props) {
				if("name".equals(prop.getName()) || "g".equals(prop.getName())) {
					continue;
				}
				propString += "\n\t\t\t\t" +prop.getName() + ":\"" + prop.getStringValue()+"\",";
			}
			if(propString.length() > 0)
				propString = propString.substring(0, propString.length()-1);
			
			String nodeId = "rect" + (++count);
			nodeNameMap.put(node.attributeValue("name"), nodeId);
			String nodeJson = "\n\t\t"+nodeId+" : {" +
			"\n\t\t\ttype : '"+node.getName()+"'," +
			"\n\t\t\ttext : {" +
			"\n\t\t\t	text : '"+node.attributeValue("name")+"'" +
			"\n\t\t\t}," +
			"\n\t\t\tattr : {" +
			"\n\t\t\t	x : "+positionInfo[0]+"," +
			"\n\t\t\t	y : "+positionInfo[1]+"," +
			"\n\t\t\t	width : "+positionInfo[2]+"," +
			"\n\t\t\t	height : "+positionInfo[3]+"" +
			"\n\t\t\t}," +
			"\n\t\t\tprops:{" +
			propString + 
			"\n\t\t\t}" +
			"\n\t\t}";
			result.add(nodeJson);
		}
		
		return result;
	}
	private List<String> toPathJson(Element root) {
		List<String> result = new ArrayList<String>();
		
		for (Object element : root.elements()) {
			Element node = (Element) element;
			
			List<Element> paths = node.elements("transition");
			if(paths!=null) {
				for (Element path : paths) {
					String g = path.attributeValue("g");
					String[] info = g.split(":");
					String textPosX = "";
					String textPosY = "";
					String dotsString = "";
					if(info.length == 1) {
						String[] textPos = info[0].split(",");
						textPosX = textPos[0];
						textPosY = textPos[1];
					} else {
						String[] textPos = info[1].split(",");
						textPosX = textPos[0];
						textPosY = textPos[1];
						
						String[] dotsInfo = info[0].split(";");
						for(String dotInfo: dotsInfo) {
							String[] dotPosInfo = dotInfo.split(",");
							dotsString += "{x: "+dotPosInfo[0]+", y: "+dotPosInfo[1]+"},";
						}
					}
					if(dotsString.length()>0)
					dotsString = dotsString.substring(0, dotsString.length()-1);
					
					String fromNodeId = nodeNameMap.get(node.attributeValue("name"));
					String toNodeId = nodeNameMap.get(path.attributeValue("to"));
					String pathName = path.attributeValue("name");
					if(pathName == null) 
						pathName = "";
					String pathStr = "\n\t\tpath"+(++count)+" : {"
						+"\n\t\t\tfrom : '"+fromNodeId+"',"
						+"\n\t\t\tto : '"+toNodeId+"',"
						+"\n\t\t\tdots : ["+dotsString+"],"
						+"\n\t\t\ttext : {"
						+"\n\t\t\t	text : '"+pathName+"'"
						+"\n\t\t\t},"
						+"\n\t\t\ttextPos : {"
						+"\n\t\t\t	x : "+textPosX+","
						+"\n\t\t\t	y : "+textPosY
						+"\n\t\t\t},"
						+"\n\t\t\tprops : {"
						+"\n\t\t\t	text : {"
						+"\n\t\t\t		value : '"+path.attributeValue("name")+"'"
						+"\n\t\t\t	}"
						+"\n\t\t\t}"
						+"\n\t\t}";
					result.add(pathStr);
				}
			}
		}
		return result;
	}
	public String toJson(String jpdlXml) {
		
		Document document = null;
		try {
			document = DocumentHelper.parseText(jpdlXml);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		Element root = document.getRootElement();
		List<String> nodeJsons = toNodeJson(root);
		List<String> pathJsons = toPathJson(root);
		
		String result = "{\n\t";
		result += "\n\tstates : {";
		boolean isFirst = true;
		for(String nodeJson : nodeJsons) {
			if(isFirst) {
				result += nodeJson;
				isFirst = false;
			} else {
				result += "," + nodeJson;
			}
		}
		result+="\n\t},";
		result += "\n\tpaths : {";
		isFirst = true;
		for(String pathJson : pathJsons) {
			if(isFirst) {
				result += pathJson;
				isFirst = false;
			} else {
				result += "," + pathJson;
			}
		}
		
		result+="\n\t}";
		result += ","
				+"\n\tprops : {"
				+"\n\t	props : {"
				+"\n\t		name : {"
				+"\n\t			value : '"+root.attributeValue("name")+"'"
				+"\n\t		},"
				+"\n\t		key : {"
				+"\n\t			value : ''"
				+"\n\t		},"
				+"\n\t		desc : {"
				+"\n\t			value : ''"
				+"\n\t		}"
				+"\n\t	}"
				+"\n\t}";
		result+="\n}";
		return result;
	}
}
